package com.feige.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.constant.Constants;
import com.feige.dao.ClazzMapper;
import com.feige.openfeigh.UserService;
import com.feige.pojo.Clazz;
import com.feige.pojo.User;
import com.feige.pojo.vo.CourseVo;
import com.feige.result.R;
import com.feige.service.ClazzService;
import com.feige.utils.aliyun.AliyunOssHandler;
import com.feige.utils.inventcode.ShareCodeUtil;
import com.feige.utils.qrcode.QrCodeUtils;
import com.feige.utils.redis.RedisUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserService userService;

    @Override
    public boolean createClazz(Clazz clazz) {
        String fileName = IdUtil.simpleUUID() + ".jpg";
        String name = AliyunOssHandler.joinFileName(QrCodeUtils.CLAZZ_FILE_TYPE_NAME, fileName);
        String inventQrCodePath = AliyunOssHandler.BUCKET_URL + name;
        clazz.setInventQrCodePath(inventQrCodePath);
        boolean save = this.save(clazz);
        boolean b = QrCodeUtils.generateQrCodeSaveOss(Constants.CLAZZ_QR_CODE_URL_PREFIX + clazz.getId(), name);
        return save && b;
    }

    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteClazzById(String id) {
        // 先删除班级
        boolean remove = this.removeById(id);
        // 把选了该课程的学生班级设置为默认值
        R<User> update = userService.updateDefault(id);
        return update.getCode() == 200 && remove;
    }

    @Override
    public CourseVo getClazzByInvestCode(String code) {
        return this.baseMapper.getClazzByInvestCode(ShareCodeUtil.codeToId(code));
    }

    @Override
    public CourseVo getClazzVoById(String id) {
        return this.baseMapper.getClazzVoById(id);
    }

    @Override
    public User getUserByClazzId(String clazzId) {
        return this.baseMapper.getUserByClazzId(clazzId);
    }

    @Override
    public Clazz getClazzById(String id) {
        String key = Constants.CLASS_KEY + id;
        Clazz clazz = (Clazz)redisUtils.get(key);
        if (ObjectUtil.isNotNull(clazz)){
            return clazz;
        }else {
            clazz = this.getById(id);
            if (ObjectUtil.isNotNull(clazz)){
                clazz.setInventCode(ShareCodeUtil.toSerialCode(clazz.getInventCode()));
                // 设置随机过期时间
                int randomInt = RandomUtil.randomInt(1,10);
                redisUtils.set(key,clazz,randomInt * 2 * 60 * 60);
                return clazz;
            }else {
                return null;
            }
        }
    }

    @Override
    public boolean incrementBy(String clazzId) {
        return this.baseMapper.incrementBy(clazzId) > 0;
    }

    @Override
    public boolean decrementBy(String clazzId) {
        return this.baseMapper.decrementBy(clazzId) > 0;
    }


}
